package com.gonfre.quientelo;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.StrictMode;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.gc.materialdesign.views.ButtonFlat;
import com.gc.materialdesign.views.ProgressBarCircularIndeterminate;
import com.gc.materialdesign.widgets.Dialog;

import util.AsyncRequest;
import util.Request;
import util.Response;


public class LoginActivity extends MyActivity {
    private ProgressBarCircularIndeterminate progressBarLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        //getMenuInflater().inflate(R.menu.menu_login, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    public void buttonTestAppOnClick(View w) {
        Intent intent = new Intent(LoginActivity.this, RequestPriceActivity.class);

        //intent.putExtra("activityToBack", "Login");
        startActivity(intent);
        //finish();
    }

    public void buttonRegisterAsSellerOnClick(View w) {
        Intent intent = new Intent(LoginActivity.this, RegisterSellerActivity.class);

        //intent.putExtra("activityToBack", "Login");
        startActivity(intent);
        //finish();
    }

    public void buttonRegisterAsPurchaserOnClick(View w) {
        Intent intent = new Intent(LoginActivity.this, RegisterPurchaserActivity.class);

        //intent.putExtra("activityToBack", "Login");
        startActivity(intent);
        //finish();
    }

    public void buttonLoginOnClick(View w) {
        startProgressBarCircular(R.id.progressBarLoginLayout);

        TextView t0 = (TextView)findViewById(R.id.editTextEmail);
        String t00 = t0.getText().toString();
        TextView t1 = (TextView)findViewById(R.id.editTextPassword);
        String t11 = t1.getText().toString();

        AsyncRequest req = new AsyncRequest(this);
        Response res = new Response();

        res = req.xLogin(t00, t11);
    }

    public void buttonForgotPasswordOnClick(View w) {
        Intent intent = new Intent(LoginActivity.this, RequestListActivity.class);

        //intent.putExtra("activityToBack", "Login");
        startActivity(intent);
        //finish();
    }

    @Override
    public void onFinishRequest(int type, Response res) {
        stopProgressBarCircular(R.id.progressBarLoginLayout);
        Dialog dialog;
        if(res.getHeaderResponse() == 1){
            // Se modifica el key actual
            setKey(res.getBodyValueStr("key"));
        }

        dialog = new Dialog(this,null, res.getHeaderResponse().toString() + "-" + res.getHeaderMessage());
        dialog.show();
    }

}
